package com.zxf.course.controller;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.zxf.course.bean.Student;
import com.zxf.course.commonutils.ImportExcelUtil;
import com.zxf.course.commonutils.TimeConvert;
import com.zxf.course.service.ClassesService;
import com.zxf.course.service.ExcelOperateService;
import com.zxf.course.service.StudentService;

@Controller
@RequestMapping("/userManager")
public class ExcelOperateAction {
	@Autowired
	private ExcelOperateService excelService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClassesService classesService;
	
	@RequestMapping("/export")
	@ResponseBody
	public String export(HttpServletResponse response) {
		response.setContentType("application/binary;charset=UTF-8");
		try {
			ServletOutputStream out = response.getOutputStream();
			String fileName = new String(
					("studentInfo " + new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(), "UTF-8");
			response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
			String[] titles = { "学号", "姓名", "联系方式", "班级名称", "注册时间" };
			excelService.exportExcel(titles, out);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "导出信息失败";
		}
	}
	@RequestMapping("/uploadOperate")
	public String sendToUpdatePage() {
		return "uploadExcel";
	}
	
	@RequestMapping("/uploadExcel")
	@ResponseBody
	public void ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
		MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);    
        InputStream in =null;
        List<List<Object>> listDatas = null;  
        MultipartFile file = multipartRequest.getFile("upfile");  
        if(file.isEmpty()){  
            throw new Exception("文件不存在！");
        }  
        in = file.getInputStream(); 
        listDatas = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
        in.close(); 
        
        for (int i = 0; i < listDatas.size(); i++) {  
            List<Object> item = listDatas.get(i);
            Student student = new Student();
            student.setStudentNo(Integer.parseInt((String.valueOf(item.get(0)))));
            student.setStudentName(String.valueOf(item.get(1)));
            student.setStudentContact(String.valueOf(item.get(2)));
            student.setClassNo(classesService.selectClassesByName(String.valueOf(item.get(3))));
            student.setAddTime(TimeConvert.strToDate(String.valueOf(item.get(4))));
            studentService.addStudent(student);
         }
	} 
}
